<?php

namespace App\Http\Controllers\Admin;

use App\Exports\OrderExport;
use App\Http\Response;
use App\Jobs\DeliveryJob;
use App\Order;
use App\Services\ExpressService;
use App\Sku;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;

class OrderController extends Controller
{
    use Response;

    public function index(Request $request)
    {

        $paginate = Order::with(['detail','user'])->when($request->get('order_number'), function ($query) use ($request) {
            $query->where('order_number', '=', $request->get('order_number'));
        })->when($request->get('name'), function ($query) use ($request) {
            $query->where('address_consignee', 'like', '%' . $request->get('name') . '%');

        })->when($request->get('status'), function ($query) use ($request) {
            switch ($request->get('status')) {
                case 0:
                    return $query;
                    break;
                case 1:
                    return $query->where('status', -1);
                    break;
                case 2:
                    return $query->where([['status', '=', 0]]);
                    break;
                case 3:
                    return $query->where([['status', '=', 1]])->orWhere('status','=',2);
                    break;
                case 4:
                    return $query->where([['status','=',1],['deliveryStatus','=',0]]);
                    break;
                case 5:
                    return $query->where([['status','=',1],['deliveryStatus','=',1],['receivingStatus','=',0]])->orWhere([['status','=',2],['deliveryStatus','=',1],['receivingStatus','=',0]]);
                    break;
                case 6:
                    return $query->where([['status','=',1],['deliveryStatus','=',1],['receivingStatus','=',1]])->orWhere([['status','=',2],['deliveryStatus','=',1],['receivingStatus','=',1]]);
                    break;
//                case 4:
//                    return $query->where([['status','=',-1]]);
//                    break;
            }
        })->latest()->paginate($request->get('pageSize'), '*', 'pageNo');
        return $this->success(200, $paginate);
    }

    public function update($id, Request $request)
    {
        $data = $request->all();
        $find = Order::with('detail')->find($id);

        $update = Order::where('id', $id)->update($data);

        if ($update) {
            return $this->message(200, '更新成功');
        } else {
            return $this->error(505, '更新失败');
        }
    }

    public function show($id)
    {
        $data = Order::with('detail')->where('id', $id)->first();
        return $this->success(200, $data);
    }

    public function delivery(Request $request)
    {
        $data = $request->get('ids');
        $this->dispatch(new DeliveryJob($data));
        return $this->message(200,'发货成功');
    }

    public function export(Request $request)
    {
        $start_time = $request->get('start_time');
        $end_time= $request->get('end_time');
        $status=$request->get('status');
        return Excel::download(new OrderExport($start_time,$end_time,$status), 'order.xlsx');
    }

    public function cms()
    {
        $total_order = Order::get();
        $today_order = Order::whereDay('created_at', date('d', time()))->get();
        $unpaid_order_number = $total_order->where('status', 0)->count();
        $paid_and_no_delivery_number = $total_order->where('status', 1)->where('deliveryStatus', 0)->count();
        $delivery_and_no_receive_number = $total_order->where('status', 1)->where('deliveryStatus', 1)->where('receivingStatus', 0)->count();
        $complete_number=$total_order->where('status', 1)->where('deliveryStatus', 1)->where('receivingStatus', 1)->count();
        $today_sales=round($today_order->where('status',1)->sum("pay_amount"),2);
        $delivery=$total_order->where('status',1)->where('deliveryStatus', 1)->count();
        $total_sales=round($total_order->where('status',1)->sum('pay_amount'),2);


        $result=[
            'today_order_number'=>$today_order->count(),
            'today_sales'=>$today_sales,
            'unpaid_order_number'=>$unpaid_order_number,
            'paid_and_no_delivery_number'=>$paid_and_no_delivery_number,
            'delivery_and_no_receive_number'=>$delivery_and_no_receive_number,
            'complete_number'=>$complete_number,
            'total_sales'=>$total_sales,
            'delivery'=>$delivery
        ];
        return $this->success(200,$result);

    }
}
